package com.manager.commons.encrypt.des;

//                   _ooOoo_    
//                  o8888888o    
//                  88" . "88    
//                  (| -_- |)    
//                   O\ = /O    
//               ____/'---'\____    
//              /. '\\| : |//' .\    
//             // \\||| : |||// \\   
//            // _|||||-:-|||||_ \\    
//           // | | \\\ - /// | | \\
//           || |_| ''\---/'' |_| || 
//            \ .-\___/'-'\___/-. /    
//          ___\. .' /--.--\ '. ./___    
//        .""'< `.___\_<|>_/___.' >'"".    
//       | |: `- \`.|'\ _ /'|.`/ -` :| |    
//        \ \ `-. \_ __\ /__ _/ .-` / /    
//======'-.___`-.___\_______/___.-`___.-'======    
//                   '=---='    
//.................................................    
//写一哈代码，你就青叫唤；信龟儿设计，你逗是傻逼。
//永无Bug，啷个可能！求佛主保佑也没球用！

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import com.manager.commons.encrypt.base64.Base64;
import com.manager.commons.utils.StringUtil;

/**
 * Des3
 * @author H
 * @version V1.0
 *
 */
public class Des3 {

	/**
	 * 密钥 24位
	 */
	private final static String defKey = "flzx3qc@aniaml#y4yhl9t#$";
	/**
	 * 向量
	 */
	private final static String iv = "01234567";
	/**
	 * 加解密统一使用的编码方式
	 */
	private final static String _character = "utf-8";

	/**
	 * 编码
	 * @param text
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String encode(String text, String key) throws Exception {
		return encode(text, key, "");
	}

	/**
	 * 编码
	 * @param text
	 * @param key
	 * @param ivps
	 * @return
	 * @throws Exception
	 */
	public static String encode(String text, String key, String ivps) throws Exception {
		return encode(text, key, ivps, _character);
	}

	/**
	 * 编码
	 * @param text
	 * @param key
	 * @param ivps
	 * @param character
	 * @return
	 * @throws Exception
	 */
	public static String encode(String text, String key, String ivps, String character) throws Exception {
		Key deskey = null;
		byte[] keys = StringUtil.isNotEmpty(key) ? key.getBytes() : defKey.getBytes();

		DESedeKeySpec spec = new DESedeKeySpec(keys);
		SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
		deskey = keyfactory.generateSecret(spec);
		Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");

		byte[] _ivps = StringUtil.isNotEmpty(ivps) ? ivps.getBytes() : iv.getBytes();
		cipher.init(Cipher.ENCRYPT_MODE, deskey, new IvParameterSpec(_ivps));
		return Base64.encode(cipher.doFinal(text.getBytes(StringUtil.isNotEmpty(character) ? character : _character)));
	}

	/**
	 * 解码
	 * @param text
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String decode(String text, String key) throws Exception {
		return decode(text, key, "");
	}

	/**
	 * 解码
	 * @param text
	 * @param key
	 * @param ivps
	 * @return
	 * @throws Exception
	 */
	public static String decode(String text, String key, String ivps) throws Exception {
		return decode(text, key, ivps, _character);
	}

	/**
	 * 解码
	 * @param text
	 * @param key
	 * @param ivps
	 * @param character
	 * @return
	 * @throws Exception
	 */
	public static String decode(String text, String key, String ivps, String character) throws Exception {
		Key deskey = null;

		byte[] secretKeys = StringUtil.isNotEmpty(key) ? key.getBytes() : defKey.getBytes();
		DESedeKeySpec spec = new DESedeKeySpec(secretKeys);
		SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
		deskey = keyfactory.generateSecret(spec);
		Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");

		byte[] _ivps = StringUtil.isNotEmpty(ivps) ? ivps.getBytes() : iv.getBytes();
		cipher.init(Cipher.DECRYPT_MODE, deskey, new IvParameterSpec(_ivps));
		return new String(cipher.doFinal(Base64.decode(text)), StringUtil.isNotEmpty(character) ? character : _character);
	}
}